﻿Imports DotNetNuke.Services.Localization
Imports DotNetNuke.Entities.Modules
Imports System.Linq
Imports System.Collections.Generic
Imports DotNetNuke.Services.Authentication
Imports System.Drawing

Namespace DesktopModules.DnnSecurity

    Partial Class DnnSecurity
        Inherits PortalModuleBase

        Private _dnnSecurityChecks As New Generic.List(Of IDnnSecurityCheck)
        Private _checksRessource As String = Localization.GetResourceFile(Me, "Checks")
        Private _resultRessource As String = Localization.GetResourceFile(Me, "Results")


        Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            _dnnSecurityChecks.Add(New Checks.AuthenticationHost)
            _dnnSecurityChecks.Add(New Checks.AuthenticationUser)
            '_dnnSecurityChecks.Add(New Checks.MembershipUserSuperUserRatio)
            _dnnSecurityChecks.Add(New Checks.WebAppTrustLevel)
            _dnnSecurityChecks.Add(New Checks.DataBasePermission)
            _dnnSecurityChecks.Add(New Checks.UpToDate)


            RadChart1.HttpHandlerUrl = ResolveUrl("ChartImage.axd")
        End Sub


        Protected ReadOnly Property AuthenticationEnabled() As Boolean
            Get
                Return AuthenticationConfig.GetConfig(PortalId).Enabled
            End Get
        End Property



        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            PanelResults.Visible = True

            'init stats
            Dim stats As New Dictionary(Of SecurityRiskClassification, Integer)
            For Each level In [Enum].GetValues(GetType(SecurityRiskClassification))
                stats.Add(level, 0)
            Next

            If AuthenticationEnabled Then

                For Each check As IDnnSecurityCheck In _dnnSecurityChecks
                    check.LoadRessourceFile(_checksRessource)
                    check.Execute(Me.PortalSettings, Nothing)

                    For Each res As IDnnSecurityResult In check.Results
                        stats(res.SecurityRiskClassification) += 1
                    Next
                Next

                Dim statSum As Integer = stats.Sum(Function(x) x.Value)

                rptResults.DataSource = _dnnSecurityChecks
                rptResults.DataBind()


                Dim chartSeries As New Telerik.Charting.ChartSeries
                chartSeries.Type = Telerik.Charting.ChartSeriesType.Pie
                chartSeries.Name = ""
                chartSeries.Appearance.LegendDisplayMode = Telerik.Charting.ChartSeriesLegendDisplayMode.Nothing


                For Each level In [Enum].GetValues(GetType(SecurityRiskClassification))
                    Dim chartItem As New Telerik.Charting.ChartSeriesItem
                    chartItem.Name = level.ToString
                    chartItem.YValue = stats(level)
                    chartItem.Appearance.FillStyle.MainColor = GetIndicatorColor(level)
                    chartItem.Appearance.FillStyle.SecondColor = GetIndicatorColor(level, True)

                    chartSeries.Items.Add(chartItem)

                    Dim labelItem As New Telerik.Charting.LabelItem
                    labelItem.TextBlock.Text = level.ToString
                    labelItem.Marker.Appearance.FillStyle.MainColor = GetIndicatorColor(level)
                    labelItem.Marker.Appearance.Border.Color = GetIndicatorColor(level, True)
                    labelItem.Marker.Appearance.Figure = "Diamond"
                    RadChart1.Legend.Items.Add(labelItem)
                Next

                RadChart1.Series.Add(chartSeries)
            Else

                Throw New Exception("Unauthorized.")

            End If


        End Sub


        Protected Sub rptResults_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
            Dim rptResults As Repeater = e.Item.FindControl("rptResults")
            Dim pnlIndicator As Panel = e.Item.FindControl("pnlIndicator")


            If rptResults IsNot Nothing Then
                Dim check As IDnnSecurityCheck = CType(e.Item.DataItem, IDnnSecurityCheck)

                If pnlIndicator IsNot Nothing Then
                    pnlIndicator.BackColor = GetIndicatorColor(check.SecurityRiskClassification)
                    pnlIndicator.BorderColor = GetIndicatorColor(check.SecurityRiskClassification, True)
                End If

                For Each result As IDnnSecurityResult In check.Results
                    result.LoadRessourceFile(_resultRessource)
                Next

                AddHandler rptResults.ItemDataBound, AddressOf rptChecks_ItemDataBound
                rptResults.DataSource = check.Results
                rptResults.DataBind()
            End If

        End Sub

        Protected Sub rptChecks_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
            Dim pnlSecurityResult As Panel = e.Item.FindControl("pnlSecurityResult")

            If pnlSecurityResult IsNot Nothing Then
                Dim result As IDnnSecurityResult = CType(e.Item.DataItem, IDnnSecurityResult)

                pnlSecurityResult.BackColor = GetIndicatorColor(result.SecurityRiskClassification)
                pnlSecurityResult.BorderColor = GetIndicatorColor(result.SecurityRiskClassification, True)
            End If
        End Sub


        Private Function GetIndicatorColor(ByVal level As SecurityRiskClassification, Optional ByVal alterColor As Boolean = False) As System.Drawing.Color
            Select Case level
                Case SecurityRiskClassification.Ok
                    If alterColor Then Return Color.AliceBlue
                    Return Color.LightBlue
                Case SecurityRiskClassification.Low
                    If alterColor Then Return Color.LightGreen
                    Return Color.Green
                Case SecurityRiskClassification.Middle
                    If alterColor Then Return Color.LightYellow
                    Return Color.Yellow
                Case SecurityRiskClassification.High
                    If alterColor Then Return Color.Orange
                    Return Color.OrangeRed
                Case SecurityRiskClassification.Critical
                    If alterColor Then Return Color.IndianRed
                    Return Color.Red
            End Select
        End Function
    End Class
End Namespace